package com.qyer.search.storm.logging;

import org.apache.log4j.DailyRollingFileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.RollingFileAppender;

/**
 * Created by tangzheng on 16/1/28.
 */
public class StormLogger {
  protected String fileName;
  protected String loggerName;
  protected String pattern;
  protected Level level;
  protected Logger logger;


  public StormLogger(String fileName, String loggerName, String pattern, Level level) {
    try {
      this.fileName = fileName;
      this.loggerName = loggerName;
      this.pattern = pattern;
      this.level = level;

      PatternLayout patternLayout = new PatternLayout();
      patternLayout.setConversionPattern(pattern);

      DailyRollingFileAppender appender = new DailyRollingFileAppender();
      //RollingFileAppender rollingFileAppender = new RollingFileAppender();
      appender.setFile(fileName);
      appender.setLayout(patternLayout);
      appender.setAppend(true);
      appender.activateOptions();

      logger = Logger.getLogger(loggerName);
      logger.setLevel(level);
      logger.addAppender(appender);
    }catch (Exception e){
      e.printStackTrace();
    }

  }

  public void info(String str){
    if(level == Level.INFO){
      logger.info(str);
    }else if(level == Level.ERROR){
      logger.error(str);
    }else if(level == Level.WARN){
      logger.warn(str);
    }
  }
}
